package com.team2.outpatient_sys.web.pharmacyControllers;


import com.github.pagehelper.PageInfo;
import com.team2.outpatient_sys.pojo.Dispense;
import com.team2.outpatient_sys.pojo.Medicine;
import com.team2.outpatient_sys.pojo.ProvideMedicine;
import com.team2.outpatient_sys.pojo.Result;
import com.team2.outpatient_sys.service.ProvideMedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/provideMedicine")
public class ProvideMedicineController {

    @Autowired
    private ProvideMedicineService provideMedicineService;

    @PostMapping("/add")
    public Result add(@RequestBody ProvideMedicine  provideMedicine){
        Result result = new Result();
        if (provideMedicineService.add(provideMedicine)) {
            result.setCode(200);
            result.setMsg("增加成功");
        } else {
            result.setCode(500);
            result.setMsg("增加失败");
        }
        return result;
    }


    @PutMapping("/update")
    public Result update(@RequestBody ProvideMedicine  provideMedicine){
        Result result = new Result();
        if (provideMedicineService.update(provideMedicine)) {
            result.setCode(200);
            result.setMsg("修改成功");
        } else {
            result.setCode(500);
            result.setMsg("修改失败");
        }
        return result;
    }

    @GetMapping
    public Result findAll(){
        List<ProvideMedicine> list=provideMedicineService.findAll();
        Result result=new Result();
        if(list !=null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        ProvideMedicine provideMedicine=provideMedicineService.findById(id);
        Result result=new Result();
        if(provideMedicine !=null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(provideMedicine);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean flag=provideMedicineService.delete(id);
        Result result = new Result();
        if (flag) {
            result.setCode(200);
            result.setMsg("删除成功");
        } else {
            result.setCode(500);
            result.setMsg("删除失败");
        }
        return result;
    }


//    @GetMapping("/findProvideMedicine/{patientId}/{dispenseId}")
//    public Result findProvideMedicine(@PathVariable Integer patientId,@PathVariable Integer dispenseId){
//        List<Dispense> list= provideMedicineService.findProvideMedicine(patientId,dispenseId);
//        Result result=new Result();
//        if(list !=null){
//            result.setCode(200);
//            result.setMsg("查询成功");
//            result.setData(list);
//        }else{
//            result.setCode(500);
//            result.setMsg("查询失败");
//        }
//        return result;
//    }
//
//    @GetMapping("/{currentPage}/{pageSize}")
//    public Result findByPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize) {
//        List<Dispense> list = provideMedicineService.findByPage(currentPage, pageSize);
//        Result result = new Result();
//        if (list != null) {
//            PageInfo<Dispense> pageInfo = new PageInfo<>(list);
//            result.setCode(200);
//            result.setMsg("查询成功");
//            result.setData(pageInfo);
//        } else {
//            result.setCode(500);
//            result.setMsg("查询失败");
//        }
//        return result;
//    }

}
